using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodePlex.SpaceBlock.FS;
using CodePlex.Resourceful;
using CodePlex.Resourceful.Amazon.S3;
using System.Threading;

namespace CodePlex.SpaceBlock.UI.Controls
{
    public partial class GenerateSignedUrlForm : CodePlex.SpaceBlock.UI.Controls.BaseForm
    {
        private S3FileSystem _s3fs;
        private Path _selectedFile;
        private bool _formClosed;
        private TimeSpan? _currentSpan;

        public GenerateSignedUrlForm()
        {
            InitializeComponent();

            this.MinimumSize = this.Size;

            _descriptionLabel.Text = Properties.Messages.GenerateSignedUrl_Description;

            this.InBackground(this.BackgroundLoop);

            _expireCombo.SelectedItem = "Minutes";

            this.FormClosed += delegate
            {
                _formClosed = true;
            };


        }

        public void Initialize(S3FileSystem s3fs, Path selectedFile)
        {
            _s3fs = s3fs;
            _selectedFile = selectedFile;

            this.Text = string.Format(Properties.Messages.GenerateSignedUrl_Title, _selectedFile.Name);
        }






        private void SetUrl(string text)
        {
            this.InDisplay(delegate
           {
               _urlBox.Text = text;
           });
        }

        private void BackgroundLoop()
        {
            while (!_formClosed)
            {
                if (_s3fs != null && _selectedFile != null && _currentSpan != null) 
                {

                    if (_currentSpan > TimeSpan.Zero)
                    {
                        try
                        {
                            string url = _s3fs.GetSignedUrl(_selectedFile, _currentSpan.Value);

                            SetUrl(url);
                        }
                        catch (Exception ex)
                        {
                            SetUrl("Error: " + ex.Message);
                        }
                    }
                    else
                    {
                        SetUrl("");
                    }

                    _currentSpan = null;

                }

                Thread.Sleep(500);
            }
                
        }
 
        private void _generateButton_Click(object sender, EventArgs e)
        {
            SetUrl("Generating...");

            double num = default(double);
            if (double.TryParse(_expireBox.Text, out num))
            {
                if (num > 0)
                {
                    Dictionary<string, Converter<double, TimeSpan>> map = new Dictionary<string, Converter<double, TimeSpan>>();
                    map["Seconds"] = TimeSpan.FromSeconds;
                    map["Minutes"] = TimeSpan.FromMinutes;
                    map["Hours"] = TimeSpan.FromHours;
                    map["Days"] = TimeSpan.FromDays;

                    _currentSpan = map[_expireCombo.SelectedItem as string](num);
                    return;
                }
            }

            _currentSpan = TimeSpan.Zero;
            
        }
    }
}

